package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.beans.GroupLevelBean;
import com.yuyh.library.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/8/21.
 */

public class GroupLevelAdapter extends RecyclerView.Adapter <GroupLevelAdapter.ViewHold>{
    private static final String TAG = "SendCircleActivity";
    private Context mContext ;
    private List<GroupLevelBean> datas ;
    private String thisLevel = "0" ;
    private int type = 0 ;//0=升级；1=续费

    public GroupLevelAdapter(Context mContext, List<GroupLevelBean> datas){
        this.mContext = mContext ;
        this.datas = (null==datas)?new ArrayList<GroupLevelBean>():datas ;
//        this.datas = datas ;
    }

    public void setType(int type){
        this.type = type ;
    }
    public void setThisLevel(String thisLevel){
        this.thisLevel = thisLevel ;
    }
    private OnClickListener onClickListener ;
    public static interface OnClickListener {
        void OnClick(int type);
    }
    public void setOnclick(OnClickListener onclick){
        onClickListener = onclick ;
    }

    @Override
    public GroupLevelAdapter.ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View headView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_level, parent, false);
        return new ViewHold(headView);
    }

    @Override
    public void onBindViewHolder(GroupLevelAdapter.ViewHold holder, int position) {
//        GlideUtils.loadCircleImage(mContext,datas[position],holder.image_head);

        final GroupLevelBean groupLevelBean = datas.get(position);
        if ("1".equals(groupLevelBean.getLevel())){
            holder.txt_name.setText("一级群-500人");
        }else if ("2".equals(groupLevelBean.getLevel())){
            holder.txt_name.setText("二级群-1000人");
        }else if ("3".equals(groupLevelBean.getLevel())){
            holder.txt_name.setText("超级群-2000人");
        /*}else if ("4".equals(groupLevelBean.getLevel())){
            holder.txt_name.setText("超级群-2000人");*/
        }

        boolean isOnclick = false ;
        holder.txt_group_up.setText("升级");
        if ("0".equals(thisLevel)){
            holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
            isOnclick = true ;
        }else if ("1".equals(thisLevel)){
            if ("1".equals(groupLevelBean.getLevel())){
                if (1 ==type||type==0){
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                    holder.txt_group_up.setText("续费");
                    isOnclick = true ;
                }else {
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                    isOnclick = false ;
                }
            }else {
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                isOnclick = true ;
            }
        }else if ("2".equals(thisLevel)){
            if ("1".equals(groupLevelBean.getLevel())){
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                isOnclick = false ;
            }else if ("2".equals(groupLevelBean.getLevel())){
                if (1 ==type||type==0){
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                    holder.txt_group_up.setText("续费");
                    isOnclick = true ;
                }else {
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                    isOnclick = false ;
                }
            }else {
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                isOnclick = true ;
            }
        }else if ("3".equals(thisLevel)){
            if ("1".equals(groupLevelBean.getLevel())){
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                isOnclick = false ;
            }else if ("2".equals(groupLevelBean.getLevel())){
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                isOnclick = false ;
            }else if ("3".equals(groupLevelBean.getLevel())){
                if (1 ==type||type==0){
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                    holder.txt_group_up.setText("续费");
                    isOnclick = true ;
                }else {
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                    isOnclick = false ;
                }
            }else if ("4".equals(groupLevelBean.getLevel())){
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                isOnclick = true ;
            }
        }else if ("4".equals(thisLevel)){
            if ("4".equals(groupLevelBean.getLevel())){
                if (1 ==type){
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
                    holder.txt_group_up.setText("续费");
                    isOnclick = true ;
                }else {
                    holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                    isOnclick = false ;
                }
            }else {
                holder.txt_group_up.setBackgroundColor(mContext.getResources().getColor(R.color.middle_gray_1));
                isOnclick = false ;
            }
        }

        int a = Integer.parseInt(groupLevelBean.getExpire());

        holder.txt_content.setText("期限" + (a/(60*60*24)) + "天  " +groupLevelBean.getFee()+ "元");
        if (isOnclick)
        holder.txt_group_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=onClickListener)onClickListener.OnClick(Integer.parseInt(groupLevelBean.getLevel()));
            }
        });

    }

    @Override
    public int getItemCount() {
        return null==datas?0:datas.size();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        ImageView image_head ;
        TextView txt_group_up ;
        TextView txt_name ;
        TextView txt_content ;

        public ViewHold(View itemView) {
            super(itemView);
            image_head = (ImageView) itemView.findViewById(R.id.head);
            txt_group_up = (TextView) itemView.findViewById(R.id.txt_group_up);
            txt_name = (TextView) itemView.findViewById(R.id.txt_name);
            txt_content = (TextView) itemView.findViewById(R.id.txt_content);
        }
    }

}
